/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */

package com.ecarx.systemui.plugin.common;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.provider.Settings;
import android.text.TextUtils;

import androidx.core.content.ContextCompat;

import com.ecarx.systemui.plugin.R;
import com.ecarx.systemui.plugin.utils.LogUtils;


/**
 * Created by ck on 2022/9/3 at 10:50.
 * desc: 通过主题样式获取selector
 */

public class MultiThemeUtils {
    private static final String TAG = "ThemeSelectorUtils";
    public static final String XC_THEME = "XCTheme";

    /**
     * 获取对应主题的selector选择器drawable
     * <p>
     * 切记要点：默认orange主题的图片名为 xxx.png，对应pink主题的图片应命名为xxx_pink.png
     */
    public static Drawable getSelectorDrawableByTheme(Context context, int originResoureIdNormal,
                                                      int originResoureIdPress) {
        int resultNormalId = getResourceIdByTheme(context, originResoureIdNormal);
        int resultPressId = getResourceIdByTheme(context, originResoureIdPress);

        return getSeletorDrawable(context, resultNormalId, resultPressId, -1);
    }


    /**
     * 获取对应主题的图片id
     * 目前只适配pink
     * 切记要点：默认orange主题的图片名为 xxx.png，对应pink主题的图片应命名为xxx_pink.png
     */
    public static int getResourceIdByTheme(Context context, int originResourceId) {
        String resourceEntryName = context.getResources().getResourceEntryName(originResourceId);
        resourceEntryName = getResourceNameByTheme(resourceEntryName, context);

        int resultId = getResourceIdByStrName(context,resourceEntryName);
        if (resultId == 0) {
            LogUtils.e(TAG, resourceEntryName + " resoure not found");
        }

        return resultId == 0 ? originResourceId : resultId;
    }

    public static int getResourceIdByStrName(Context context, String imageName) {
        //如果没有在"drawable"下找到imageName,将会返回0
        LogUtils.d(TAG, "getResourceIdByStrName  imageName:"+imageName);
        return context.getResources().getIdentifier(imageName, "drawable", context.getPackageName());
    }

    public static int getStringResIdByName(Context context, String strName) {
        return context.getResources().getIdentifier(strName, "string", context.getPackageName());
    }

    private static String getResourceNameByTheme(String resourceEntryName, Context context) {
        String xcTheme = Settings.Global.getString(context.getContentResolver(), XC_THEME);
        if (xcTheme == null) {
            return resourceEntryName;
        }
        switch (xcTheme) {
            case "Pink":
                resourceEntryName = resourceEntryName + "_pink";
                break;
            case "Red":
                resourceEntryName = resourceEntryName + "_red";
                break;
            case "Yellow":
                resourceEntryName = resourceEntryName + "_yellow";
                break;
            case "Edition1":
                resourceEntryName = resourceEntryName + "_edition";
                break;
            case "Blue":
                resourceEntryName = resourceEntryName + "_blue";
                break;
            default:
                break;
        }
        return resourceEntryName;
    }


    private static Drawable getSeletorDrawable(Context context, int idNormal, int idPress, int idDisable) {
        StateListDrawable stateListDrawable = new StateListDrawable();
        if (idNormal > 0) {
            Drawable normalDrawable = ContextCompat.getDrawable(context, idNormal);
            stateListDrawable.addState(new int[]{-android.R.attr.state_pressed}, normalDrawable);
        }

        if (idPress > 0) {
            Drawable pressDrawable = ContextCompat.getDrawable(context, idPress);
            stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, pressDrawable);
        }

        if (idDisable > 0) {
            Drawable disableDrawable = ContextCompat.getDrawable(context, idDisable);
            stateListDrawable.addState(new int[]{-android.R.attr.state_enabled}, disableDrawable);
        }
        return stateListDrawable;
    }

    public static int getThemeColorRes(Context context) {
        String xcTheme = Settings.Global.getString(context.getContentResolver(), XC_THEME);
        int color;
        if (xcTheme == null || xcTheme.length() == 0) {
            return R.color.text_theme_orange;
        }
        switch (xcTheme) {
            case "Pink":
                color = R.color.text_theme_pink;
                break;
            case "Red":
                color = R.color.text_theme_red;
                break;
            case "Yellow":
                color = R.color.text_theme_yellow;
                break;
            case "Edition1":
                color = R.color.text_theme_edition1;
                break;
            case "Blue":
                color = R.color.text_theme_blue;
                break;
            default:
                color = R.color.text_theme_orange;
                break;
        }
        LogUtils.i(TAG, "getThemeTextColor: " + color);
        return color;
    }
}
